Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aggfuncs: partially implement "AVG" #6951

Merged
merged 10 commits into from
Jul 3, 2018
Merged

Conversation

zz-jason
Copy link
Member

@zz-jason zz-jason commented Jul 2, 2018

What have you changed? (mandatory)

partially implement the AVG function under the new aggregate function framework, only implemented the following AVG functions:

  • AVG(DECIMAL)
  • AVG(FLOAT64)

the following AVG functions are not implemented:

  • AVG(DISTINCT DECIMAL)
  • AVG(DISTINCT FLOAT64)

What are the type of the changes (mandatory)?

  • Improvement (non-breaking change which is an improvement to an existing feature)

How has this PR been tested (mandatory)?

  • unit test
  • explain test

Does this PR affect documentation (docs/docs-cn) update? (optional)

No

Refer to a related PR or issue link (optional)

To #6952. This is the partially supported AVG functions I mentioned in #6852

@zz-jason
Copy link
Member Author

zz-jason commented Jul 2, 2018

/run-all-tests

@zz-jason zz-jason added status/all tests passed type/enhancement The issue or PR belongs to an enhancement. sig/execution SIG execution labels Jul 2, 2018
@zz-jason zz-jason added this to the 2.1 milestone Jul 2, 2018
@zz-jason
Copy link
Member Author

zz-jason commented Jul 2, 2018

@XuHuaiyu @winoros PTAL

// All the AggFunc implementations for "COUNT" are listed here.
// All the AggFunc implementations for "SUM" are listed here.
// All the AggFunc implementations for "AVG" are listed here.
_ AggFunc = (*avgOriginal4Decimal)(nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why name it Original?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

means this aggregate function is in the partial 1 or complete mode and it's input is the data from the child of logical aggregate operator, not the partial result of other aggregate function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about avg4Decimal?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avg4Decimal maybe confusing with avgPartial4Decimal, I prefer the old name.


// All the AggFunc implementations for "AVG" are listed here.
var (
_ AggFunc = (*avgOriginal4Decimal)(nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate with the code in aggfuncs.go.

_ AggFunc = (*avgOriginal4Float64)(nil)
_ AggFunc = (*avgPartial4Float64)(nil)

_ AggFunc = (*avgOriginal4Float32)(nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we wrap a castAsReal in typeInfer4Avg when the type of the arg is TypeFloat, this function signature may be removed?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. But in order to be compatible with mysql, we can not wrap a cast as real when the input parameter is a column and its type is float32

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

avg(float) returns double in MySQL.

@zz-jason
Copy link
Member Author

zz-jason commented Jul 2, 2018

@XuHuaiyu @shenli PTAL

// All the AggFunc implementations for "COUNT" are listed here.
// All the AggFunc implementations for "SUM" are listed here.
// All the AggFunc implementations for "AVG" are listed here.
_ AggFunc = (*avgOriginal4Decimal)(nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about avg4Decimal?

case aggregation.CompleteMode, aggregation.Partial1Mode:
switch aggFuncDesc.Args[0].GetType().Tp {
case mysql.TypeNewDecimal:
if aggFuncDesc.HasDistinct {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we check it at the beginning of this function?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, we only need to check this when the aggregate function is on CompleteMode or Partial1Mode

inputSum, isNull, err := e.args[1].EvalReal(sctx, row)
if err != nil {
return errors.Trace(err)
} else if isNull {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do not need the else

inputCount, isNull, err := e.args[0].EvalInt(sctx, row)
if err != nil {
return errors.Trace(err)
} else if isNull {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

@shenli
Copy link
Member

shenli commented Jul 3, 2018

LGTM

Copy link
Contributor

@XuHuaiyu XuHuaiyu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@zz-jason zz-jason added the status/LGT2 Indicates that a PR has LGTM 2. label Jul 3, 2018
@zz-jason zz-jason merged commit ef1b9df into pingcap:master Jul 3, 2018
@zz-jason zz-jason deleted the dev/agg-avg branch July 3, 2018 07:41
@@ -58,6 +59,42 @@ func buildSum(aggFuncDesc *aggregation.AggFuncDesc, ordinal int) AggFunc {

// buildCount builds the AggFunc implementation for function "AVG".
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

buildCount here may be a type?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sig/execution SIG execution status/LGT2 Indicates that a PR has LGTM 2. type/enhancement The issue or PR belongs to an enhancement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants